package com.womeyo.server;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.womeyo.mocks.Factory;
import com.womeyo.models.User;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
@RemoteServiceRelativePath("sign")
public class SignServiceImpl extends HttpServlet {
	private static final Logger log = Logger.getLogger(SignServiceImpl.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		HttpSession session = req.getSession(true);
		
//		session.setAttribute("test", "hola 33");		
				
		//if (session.isNew()) {

			User user = Factory.GetUser();
			session.setAttribute("curUser", user);			

			String tmp = req.getParameter("usernameatprofile");		
			int pos = tmp.indexOf("@");		
			if (pos > 0) {
				user.Username = tmp.substring(0, pos);

				if (tmp.length() - pos > 0)
					user.Profiles = tmp.substring(pos + 1);
			} else {
				user.Username = tmp;
				user.Profiles = "*";
			}
		//}
		
		resp.sendRedirect("/main.jsp");
	}
}